package com.yay.euro;

import com.yay.euro.model.LFType;
import com.yay.euro.model.Ticket;
import com.yay.euro.service.L10N;
import com.yay.euro.service.LocationService;
import com.yay.euro.service.TicketsService;
import com.yay.euro.tabs.TicketsAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class Contact extends Activity implements OnClickListener {
	private TextView description;
	private TextView tName;
	private TextView tDescription;
	private TextView tAddress;
	private TextView tPhone;
	private ImageView lblProcess;
	private Button btnBack;
    private static TicketsService ticketsService;
	private String s2LDescription; 
	private String s2FDescription; 
    private Drawable imgDelete;
    private Drawable imgPhone;
    private Ticket selectedTicket;
    
	private TicketsService getTicketsService() {
		if(ticketsService == null) {
			ticketsService = new TicketsService();
		}
		return ticketsService;
	}
	
	private String getS2LDescription() {
		if(s2LDescription == null) {
			s2LDescription = this.getResources().getString(R.string.s2_ldescription);
		}
		return s2LDescription;
	}
	
	private String getS2FDescription() {
		if(s2FDescription == null) {
			s2FDescription = this.getResources().getString(R.string.s2_fdescription);
		}
		return s2FDescription;
	}
	
	private TextView getDescription() {
		if(description == null) {
			description = (TextView)findViewById(R.id.description);
		    description.setTypeface(L10N.font);
		}
		return description;
	}
	
	private TextView getTName() {
		if(tName == null) {
			tName = (TextView)findViewById(R.id.lblName);
		}
		return tName;
	}
	
	private TextView getTDescription() {
		if(tDescription == null) {
			tDescription = (TextView)findViewById(R.id.lblDescription);
		}
		return tDescription;
	}
	
	private TextView getTAddress() {
		if(tAddress == null) {
			tAddress = (TextView)findViewById(R.id.lblAddress);
		}
		return tAddress;
	}
	
	private TextView getTPhone() {
		if(tPhone == null) {
			tPhone = (TextView)findViewById(R.id.lblContactPhone);
		}
		return tPhone;
	}
	
	private ImageView getLblProcess() {
		if(lblProcess == null) {
			lblProcess = (ImageView)findViewById(R.id.lblProcess);
			lblProcess.setOnClickListener(this);
		}
		return lblProcess;
	}
	
	private Drawable getImgDelete() {
		if(imgDelete == null) {
			imgDelete = this.getResources().getDrawable(R.drawable.delete);
		}
		return imgDelete;
	}

	private Drawable getImgPhone() {
		if(imgPhone == null) {
			imgPhone = this.getResources().getDrawable(R.drawable.phone);
		}
		return imgPhone;
	}
	
	private void initBtnBack() {
		if(btnBack == null) {
			btnBack = (Button) findViewById(R.id.btnBack);
			btnBack.setTypeface(L10N.font);
		    btnBack.setOnClickListener(this);
		}
	}
	
	private String S = " ";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
         L10N.init(this.getResources());
		 setContentView(R.layout.contact);

		 Ticket currentTicket = TicketsService.getCurrentTicket();
		 selectedTicket = (Ticket) this.getIntent().getExtras().get(IKeys.TICKET);
	     switch(LFType.findById(currentTicket.getType())) {
		     case LOST: getDescription().setText(getS2LDescription());
		     			break;
		     default: 	getDescription().setText(getS2FDescription());
	     }
	     
	     if(selectedTicket.getName() != null) getTName().setText(getTName().getText() + S + selectedTicket.getName());
	     if(selectedTicket.getDescription() != null) getTDescription().setText(getTDescription().getText() + S + selectedTicket.getDescription());
	     getTAddress().setText(getTAddress().getText() + S + LocationService.getAddress(getBaseContext(), selectedTicket.getLatitude(), selectedTicket.getLongitude()));
	     if(selectedTicket.getContactPhone() != null) getTPhone().setText(getTPhone().getText() + ": " + selectedTicket.getContactPhone());
	     
	     initBtnBack();
	     
	     if(currentTicket.getContactPhone()!=null && currentTicket.getContactPhone().equals(selectedTicket.getContactPhone())) {
	    	 getLblProcess().setImageDrawable(getImgDelete());
	     } else if(selectedTicket.getContactPhone() != null) {
	    	 getLblProcess().setImageDrawable(getImgPhone());
	     }
    	 getLblProcess();
	 }
	 
	 
	 private String confirmation;
	 private String yes;
	 private String no;
	 private String getConfirmation() {
		 if(confirmation == null) {
			 confirmation = this.getResources().getString(R.string.areYouSure);
		 }
		 return confirmation;
	 }
	 private String getYes() {
		 if(yes == null) {
			 yes = this.getResources().getString(R.string.yes);
		 }
		 return yes;
	 }
	 private String getNo() {
		 if(no == null) {
			 no = this.getResources().getString(R.string.no);
		 }
		 return no;
	 }
	 
	 private String tel = "tel:";
	 @Override
	 public void onClick(View arg0) {
		if(arg0.equals(getLblProcess()) && getLblProcess().getDrawable().equals(getImgDelete())) {
			AlertDialog.Builder alertbox = new AlertDialog.Builder(Contact.this);
	        alertbox.setMessage(getConfirmation());
	        alertbox.setPositiveButton(getYes(), new DialogInterface.OnClickListener() {
	        	public void onClick(DialogInterface arg0, int arg1) {
	               	// remove record
	               	getTicketsService().remove(selectedTicket);
	               	TicketsAdapter.reload = true;
	               	finish();
	            }
	        });
	        alertbox.setNegativeButton(getNo(), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface arg0, int arg1) {
	            }
	        });
	        alertbox.show();
		} else if(arg0.equals(getLblProcess()) && getLblProcess().getDrawable().equals(getImgPhone())) {
			try {
        		Intent callIntent = new Intent(Intent.ACTION_CALL);
    	        callIntent.setData(Uri.parse(tel + selectedTicket.getContactPhone()));
    	        startActivity(callIntent);
    	    } catch (ActivityNotFoundException e) {
    	        Log.e(this.getClass().getName(), e.toString());
    	    }
		} else if(arg0.equals(btnBack)) {
    		finish();
		}
	}
	 
}
